package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Logistics;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 物流Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 15:03
 */
@Repository
public class LogisticsDao extends BaseDao {
    public void saveLogistics(Logistics logistics) {
        super.save(logistics);
    }

    public void deleteLogistics(Logistics logistics) {
        super.delete(logistics);
    }

    public void updateLogistics(Logistics logistics) {
        super.update(logistics);
    }

    public void saveOrUpdateLogistics(Logistics logistics) {
        super.saveOrUpdate(logistics);
    }

    public Logistics getLogisticsById(String id) {
        return (Logistics) super.get(Logistics.class, id);
    }

    public List<Logistics> listAllLogistics() {
        String hql = "FROM Logistics WHERE isDelete = 0 ";
        return super.find(hql);
    }

    public Page<Logistics> pageByLogistics(Page<Logistics> page, Logistics logistics) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Logistics l WHERE l.isDelete = 0 ");
        if (logistics != null) {
            Map<String, Object> param = new HashMap<>();
            String customerIds = logistics.getCustomerIds();
            if (StringUtils.isNotBlank(customerIds)) {
                hqlSb.append(" AND l.customerId IN (?1) ");
                param.put("1", customerIds);
            }
            hqlSb.append(" ORDER BY l.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }
}
